# # 6.1
# import numpy as np
# import cv2
#
# canvas = np.zeros((300,300,3),np.uint8)
# canvas = cv2.line(canvas,(50,50),(250,50),(255,0,0),5)
# canvas = cv2.line(canvas,(50,150),(250,150),(0,255,0),10)
# canvas = cv2.line(canvas,(50,250),(250,250),(0,0,255),15)
# canvas = cv2.line(canvas,(150,50),(150,250),(0,255,255),20)
# cv2.imshow("Lines",canvas)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# # 6.2
# import numpy as np
# import cv2
#
# canvas = np.zeros((300,300,3),np.uint8)
# canvas = cv2.rectangle(canvas,(50,50),(200,150),(255,255,0),20)
# cv2.imshow("Rectangle",canvas)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# 6.3
# import numpy as np
# import cv2
#
# canvas = np.zeros((300,300,3),np.uint8)
# canvas = cv2.rectangle(canvas,(50,50),(250,250),(0,0,255),40)
# canvas = cv2.rectangle(canvas,(90,90),(210,210),(0,255,0),30)
# canvas = cv2.rectangle(canvas,(120,120),(180,180),(255,0,0),20)
# canvas = cv2.rectangle(canvas,(140,140),(160,160),(0,255,255),-1)
# cv2.imshow("Square",canvas)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# 6.4
# import numpy as np
# import cv2
#
# canvas = np.zeros((100,300,3),np.uint8)
# canvas = cv2.circle(canvas,(50,50),40,(0,0,255),-1)
# canvas = cv2.circle(canvas,(150,50),40,(0,255,255),-1)
# canvas = cv2.circle(canvas,(250,50),40,(0,255,0),-1)
# cv2.imshow("TrafficLights",canvas)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# 6.5
# import numpy as np
# import cv2
#
# canvas = np.zeros((300,300,3),np.uint8)
# center_X = int(canvas.shape[1]/2)
# center_Y = int(canvas.shape[0]/2)
# for r in range(0,150,30):
#     cv2.circle(canvas,(center_X,center_Y),r,(0,255,0),5)
# cv2.imshow("Circles",canvas)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# 6.6
# import numpy as np
# import cv2
#
# canvas = np.zeros((300,300,3),np.uint8)
# for numbers in range(0,28):
#     center_X = np.random.randint(0,high=300)
#     center_Y = np.random.randint(0,high=300)
#     radius = np.random.randint(11,high=71)
#     color = np.random.randint(0,high=256,size=(3,)).tolist()
#     cv2.circle(canvas,(center_X,center_Y),radius,color,-1)
# cv2.imshow("Circles",canvas)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# 6.7
# import numpy as np
# import cv2
#
# canvas = np.zeros((300,300,3),np.uint8)
# pts = np.array([[100,50],[200,50],[250,250],[50,250]],np.int32)
# canvas = cv2.polylines(canvas,[pts],True,(0,0,255),5)
# cv2.imshow("Polylines",canvas)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# 6.8
# import numpy as np
# import cv2
#
# canvas = np.zeros((100,300,3),np.uint8)
# cv2.putText(canvas,"mrsoft",(20,70),cv2.FONT_HERSHEY_TRIPLEX,2,(0,255,0),5)
# cv2.imshow("Text",canvas)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# 6.9
# import numpy as np
# import cv2
#
# canvas = np.zeros((100,300,3),np.uint8)
# fontStyle = cv2.FONT_HERSHEY_TRIPLEX + cv2.FONT_ITALIC
# cv2.putText(canvas,"mrsoft",(20,70),fontStyle,2,(0,255,0),5)
# cv2.imshow("Text",canvas)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# 6.10
# import numpy as np
# import cv2
#
# canvas = np.zeros((200,300,3),np.uint8)
# fontStyle = cv2.FONT_HERSHEY_TRIPLEX
# cv2.putText(canvas,"mrsoft",(20,70),fontStyle,2,(0,255,0),5)
# cv2.putText(canvas,"mrsoft",(20,100),fontStyle,2,(0,255,0),5,8,True)
# cv2.imshow("Text",canvas)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
#
# 6.11
# import cv2
#
# image = cv2.imread("2.png")
# fontStyle = cv2.FONT_HERSHEY_TRIPLEX
# cv2.putText(image,"Flower",(20,90),fontStyle,1,(0,255,255))
# cv2.imshow("Text",image)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
#
#6.12
# import cv2
# import time
# import numpy as np
#
# width,height = 200,200
# r = 20
# x = r+20
# y = r+100
# x_offer = y_offer = 4
#
# while cv2.waitKey(1) == -1:
#     if x > width-r or x < r:
#         x_offer *= -1
#     if y > height-r or y < r:
#         y_offer *= -1
#     x += x_offer
#     y += y_offer
#     img = np.ones((width,height,3),np.uint8)*255
#     cv2.circle(img,(x,y),r,(255,0,0),-1)
#     cv2.imshow("img",img)
#     time.sleep(1/60)
#
# cv2.destroyAllWindows()

# 7.1
import cv2
import numpy as np
img = cv2.imread("lena.bmp")
dst1=cv2.resize(img,(150,150))
dst2=cv2.resize(img,(400,400))
cv2.imshow("img",img)
cv2.imshow("dst1",dst1)
cv2.imshow("dst2",dst2)
cv2.waitKey()
cv2.destroyAllWindows()

# 7.2
import cv2
import numpy as np
img = cv2.imread("2.png")
dst3=cv2.resize(img,None,fx=1/3,fy=1/2)
dst4=cv2.resize(img,None,fx=2,fy=2)
cv2.imshow("img",img)
cv2.imshow("dst3",dst3)
cv2.imshow("dst4",dst4)
cv2.waitKey()
cv2.destroyAllWindows()
7.3
import cv2
import numpy as np
img = cv2.imread("2.png")
dst1=cv2.flip(img,0)
dst2=cv2.flip(img,1)
dst3=cv2.flip(img,-1)
cv2.imshow("img",img)
cv2.imshow("dst1",dst1)
cv2.imshow("dst2",dst2)
cv2.imshow("dst3",dst3)
cv2.waitKey()
cv2.destroyAllWindows()

# 7.4
# import cv2
# import numpy as np
# img = cv2.imread("2.png")
# rows=len(img)
# cols=len(img[0])
# M=np.float32([[1,0,50],[0,1,100]])
# det = cv2.warpAffine(img,M,(cols,rows))
# cv2.imshow("img",img)
# cv2.imshow("det",det)
# cv2.waitKey()
# cv2.destroyAllWindows()

# 7.5让图像逆时针旋转
# import cv2
# import numpy as np
# img = cv2.imread("2.png")
# rows=len(img)
# cols=len(img[0])
# center = (rows/2,cols/2)
# M=cv2.getRotationMatrix2D(center,30,0.8)
# det = cv2.warpAffine(img,M,(cols,rows))
# cv2.imshow("img",img)
# cv2.imshow("det",det)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# # 7.6让图像向右倾斜
# import cv2
# import numpy as np
# img = cv2.imread("2.png")
# rows=len(img)
# cols=len(img[0])
# p1=np.zeros((3,2),np.float32)
# p1[0]=[0,0]
# p1[1]=[cols-1,0]
# p1[2]=[0,rows-1]
# p2=np.zeros((3,2),np.float32)
# p2[0]=[50,0]
# p2[1]=[cols-1,0]
# p2[2]=[0,rows-1]
# M=cv2.getAffineTransform(p1,p2)
# det = cv2.warpAffine(img,M,(cols,rows))
# cv2.imshow("img",img)
# cv2.imshow("det",det)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 7.7模拟从图像底部得到的透视图像
# import cv2
# import numpy as np
# img = cv2.imread("2.png")
# rows=len(img)
# cols=len(img[0])
# p1=np.zeros((4,2),np.float32)
# p1[0]=[0,0]
# p1[1]=[cols-1,0]
# p1[2]=[0,rows-1]
# p1[3]=[cols-1,rows-1]
# p2=np.zeros((4,2),np.float32)
# p2[0]=[90,0]
# p2[1]=[cols-90,0]
# p2[2]=[0,rows-1]
# p2[3]=[cols-1,rows-1]
# M=cv2.getPerspectiveTransform(p1,p2)
# det = cv2.warpPerspective(img,M,(cols,rows))
# cv2.imshow("img",img)
# cv2.imshow("det",det)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 8.1二值化处理白黑渐变图
# import cv2
# img = cv2.imread("2.png",0)
# t1,dst1=cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# cv2.imshow("img",img)
# cv2.imshow("dst1",dst1)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 8.2观察不同阈值的处理效果
# import cv2
# img = cv2.imread("2.png",0)
# t1,dst1=cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# t2,dst2=cv2.threshold(img,210,255,cv2.THRESH_BINARY)
# cv2.imshow("img",img)
# cv2.imshow("dst1",dst1)
# cv2.imshow("dst2",dst2)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 8.3观察不同最大值的处理效果
# import cv2
# img = cv2.imread("2.png",0)
# t1,dst1=cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# t3,dst3=cv2.threshold(img,127,150,cv2.THRESH_BINARY)
# cv2.imshow("img",img)
# cv2.imshow("dst1",dst1)
# cv2.imshow("dst3",dst3)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 8.4对图像进行反二值化处理
# import cv2
# img = cv2.imread("2.png",0)
# t1,dst1=cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# t4,dst4=cv2.threshold(img,127,150,cv2.THRESH_BINARY_INV)
# cv2.imshow("img",img)
# cv2.imshow("dst1",dst1)
# cv2.imshow("dst4",dst4)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 8.5对图像进行低于阈值零处理
# import cv2
# img = cv2.imread("2.png",0)
# t1,dst1=cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
# cv2.imshow("img",img)
# cv2.imshow("dst1",dst1)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 8.6对图像进行超出阈值零处理
# import cv2
# img = cv2.imread("2.png",0)
# t1,dst1=cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
# cv2.imshow("img",img)
# cv2.imshow("dst1",dst1)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 8.7对图像进行截断处理
# import cv2
# img = cv2.imread("2.png",0)
# t1,dst1=cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# t2,dst2=cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
# cv2.imshow("img",img)
# cv2.imshow("dst1",dst1)
# cv2.imshow("dst2",dst2)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 8.8使用常用的5种阈值处理类型对色彩不均衡的图像进行处理
# import cv2
# img = cv2.imread("2.png",0)
# img_Gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# t1,dst1=cv2.threshold(img_Gray,127,255,cv2.THRESH_BINARY)
# t2,dst2=cv2.threshold(img_Gray,127,255,cv2.THRESH_BINARY_INV)
# t3,dst3=cv2.threshold(img_Gray,127,255,cv2.THRESH_TOZERO)
# t4,dst4=cv2.threshold(img_Gray,127,255,cv2.THRESH_TOZERO_INV)
# t5,dst5=cv2.threshold(img_Gray,127,255,cv2.THRESH_TRUNC)
# cv2.imshow("img",img)
# cv2.imshow("dst1",dst1)
# cv2.imshow("dst2",dst2)
# cv2.imshow("dst3",dst3)
# cv2.imshow("dst4",dst4)
# cv2.imshow("dst5",dst5)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 8.9使用自适应处理的效果
# import cv2
# img = cv2.imread("2.png",0)
# img_Gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# athdMEAM=cv2.adaptiveThreshold(img_Gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,5,3)
# athdGAUS=cv2.adaptiveThreshold(img_Gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,5,3)
# cv2.imshow("MEAN_C",athdMEAM)
# cv2.imshow("GAUSSIAN",athdGAUS)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 8.11
# import cv2
# img = cv2.imread("2.png",0)
# img_Gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# t1,dst1=cv2.threshold(img_Gray,127,255,cv2.THRESH_BINARY)
# t2,dst2=cv2.threshold(img_Gray,127,255,cv2.THRESH_BINARY_INV)
# cv2.imshow("img",img)
# cv2.imshow("det1",dst1)
# cv2.imshow("det2",dst2)
# cv2.waitKey()
# cv2.destroyAllWindows()

# 9.1
# import cv2
# import numpy as np
# mask=np.zeros((150,150,3),np.uint8)
# mask[50:100,20:80,:]=255
# cv2.imshow("mask1",mask)
# mask[:,:,:]=255
# mask[50:100,20:80,:]=0
# cv2.imshow("mask2",mask)
# cv2.waitKey()
# cv2.destroyAllWindows()

# 9.2 分别用”+“和add()方法计算图像和
# import cv2
# import numpy as np
# img=cv2.imread("2.png")
# sum1=img + img
# sum2=cv2.add(img,img)
# cv2.imshow("img",img)
# cv2.imshow("sum1",sum1)
# cv2.imshow("sum2",sum2)
# cv2.waitKey()
# cv2.destroyAllWindows()

# 9.3模拟三色光叠加得到白光
# import cv2
# import numpy as np
#
# img1=np.zeros((200,200,3),np.uint8)
# img1[:,:,0] =255
# img2=np.zeros((200,200,3),np.uint8)
# img2[:,:,1] =255
# img3=np.zeros((200,200,3),np.uint8)
# img3[:,:,2] =255
# cv2.imshow("img1",img1)
# cv2.imshow("img2",img2)
# cv2.imshow("img3",img3)
# img = cv2.add(img1,img2)
# cv2.imshow("1+2",img)
# img = cv2.add(img,img3)
# cv2.imshow("1+2+3",img)
# cv2.waitKey()
# cv2.destroyAllWindows()
#

# 9.4利用掩模遮盖相加结果
# import cv2
# import numpy as np
# img1=np.zeros((200,200,3),np.uint8)
# img1[:,:,0] =255
# img2=np.zeros((200,200,3),np.uint8)
# img2[:,:,2] =255
# img = cv2.add(img1,img2)
# cv2.imshow("no mask",img)
#
# m=np.zeros((200,200,1),np.uint8)
# m[50:100,50:100,:]=255
# cv2.imshow("mask",m)
#
# img3 = cv2.add(img1,img2,mask=m)
# cv2.imshow("use mask",img3)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 9.5皮卡丘图像与十字掩模做与运算
# import cv2
# import numpy as np
# img =cv2.imread("2.png")
#
# m =np.zeros(img.shape,np.uint8)
# m[100:200,:,:]=255
# m[:,100:200,:]=255
#
# img1=cv2.bitwise_and(img,m)
# cv2.imshow("pikaqu",img)
# cv2.imshow("mask",m)
# cv2.imshow("use mask",img1)
# cv2.waitKey()
# cv2.destroyAllWindows()

#
# # 9.6皮卡丘图像与十字掩模做或运算
# import cv2
# import numpy as np
# img =cv2.imread("2.png")
#
# m =np.zeros(img.shape,np.uint8)
# m[100:200,:,:]=255
# m[:,100:200,:]=255
#
# img1=cv2.bitwise_or(img,m)
# cv2.imshow("pikaqu",img)
# cv2.imshow("mask",m)
# cv2.imshow("use mask",img1)
# cv2.waitKey()
# cv2.destroyAllWindows()


# # 9.7皮卡丘图像做取反运算
# import cv2
# import numpy as np
# img =cv2.imread("2.png")
# img1=cv2.bitwise_not(img)
# cv2.imshow("pikaqu",img)
# cv2.imshow("pikaqu_not",img1)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 9.8皮卡丘图像做异或运算
# import cv2
# import numpy as np
# img =cv2.imread("2.png")
#
# m =np.zeros(img.shape,np.uint8)
# m[100:200,:,:]=255
# m[:,100:200,:]=255
#
# img1=cv2.bitwise_xor(img,m)
# cv2.imshow("pikaqu",img)
# cv2.imshow("pikaqu_xor",img1)
# cv2.imshow("mask",m)
# cv2.waitKey()
# cv2.destroyAllWindows()

# 9.9皮卡丘图像进行加密和解密
# import cv2
# import numpy as np
# def encode(img,img_key):
#     result = img = cv2.bitwise_xor(img,img_key)
#     return result
# img = cv2.imread("2.png")
# rows,colmns,channel=img.shape
#
# img_key = np.random.randint(0,256,(rows,colmns,3),np.uint8)
# cv2.imshow("1",img)
# cv2.imshow("2",img_key)
#
# result=encode(img,img_key)
# cv2.imshow("3",result)
# result=encode(result,img_key)
# cv2.imshow("4",result)
# cv2.waitKey()
# cv2.destroyAllWindows()

# # 9.10利用计算加权和的方式实现多次曝光效果
# import cv2
# sun =cv2.imread("1.jpg")
# beach = cv2.imread("4.jpg")
# rows,colmns,channel=sun.shape
# beach = cv2.resize(beach,(colmns,rows))
# img = cv2.addWeighted(sun,0.6,beach,0.6,0)
# cv2.imshow("sun",sun)
# cv2.imshow("beach",beach)
# cv2.imshow("addweightde",img)
# cv2.waitKey()
# cv2.destroyAllWindows()

# 9.11将将小猫图像覆盖到沙滩上
# import cv2
#
# cat = cv2.imread("cat.jpg")
# beach = cv2.imread("beach.jpg")
#
# cat_resized = cv2.resize(cat, (240, 213))
#
# cat1 = cat_resized[100:350, 250:490, :]
# cat2 = cv2.resize(cat_resized, (70, 160))
#
# cv2.imshow("cat", cat)
# if cat1.shape[0] > 0 and cat1.shape[1] > 0:
#     cv2.imshow("cat1", cat1)
# else:
#     print("cat1 image size is zero, cannot display")
# cv2.imshow("cat2", cat2)
#
# rows, columns, channels = cat_resized.shape
# beach[100:100 + rows, 260:260 + columns, :] = cat_resized
#
# cv2.imshow("beach2", beach)
# cv2.waitKey()
# cv2.destroyAllWindows()

# 9.12拼接禁止吸烟图像
# import cv2
#
#
# def overlay_img(img, img_over, img_over_x, img_over_y):
#     img_h, img_w, img_p = img.shape
#     img_over_h, img_over_w, img_over_c = img_over.shape
#
#     if img_over_c <= 3:
#         img_over = cv2.cvtColor(img_over, cv2.COLOR_BGR2BGRA)
#
#     for w in range(img_over_w):
#         for h in range(img_over_h):
#             if img_over[h, w, 3] != 0:
#                 for c in range(3):
#                     x = img_over_x + w
#                     y = img_over_y + h
#                     if x >= img_w or y >= img_h:
#                         break
#                     img[y, x, c] = img_over[h, w, c]
#
#     return img
#
#
# smoking = cv2.imread("smoking.jpg", cv2.IMREAD_UNCHANGED)
# no_img = cv2.imread("no.jpg", cv2.IMREAD_UNCHANGED)
#
# cv2.imshow("smoking", smoking)
# img = overlay_img(smoking, no_img, 150,200)  # 调整叠加位置
# cv2.imshow("no smoking", img)
#
# cv2.waitKey(0)
# cv2.destroyAllWindows()



